package com.ld.blecardlibrarydes.utils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 数据转换工具类
 * @author Administrator
 *
 */
public class DataUtil {

	private static StringBuffer sbTime;
	private static String now, year, month, day, hour, min, ss;
	private static SimpleDateFormat sdf = new SimpleDateFormat(
			"yyyyMMddHHmmssSSS");

	private static String resultString = null;
	private static String resultTrimData = null;

	public static DecimalFormat df = new DecimalFormat("0.00");

	/**
	 * 时间同步 取得当前的时间, 并转为需要的格式: 16进制String并以F4开头
	 */
	public static String getTimeFormat() {
		sbTime = new StringBuffer();
		now = null;
		now = sdf.format(new Date());
		year = Integer
				.toHexString(Integer.parseInt(now.substring(0, 4)) - 1900);
		now.substring(4, 6);
		month = Integer.toHexString(Integer.parseInt(now.substring(4, 6)));
		if (month.length() == 1)
			month = '0' + month;
		day = Integer.toHexString(Integer.parseInt(now.substring(6, 8)));
		if (day.length() == 1)
			day = '0' + day;
		hour = Integer.toHexString(Integer.parseInt(now.substring(8, 10)));
		if (hour.length() == 1)
			hour = '0' + hour;
		min = Integer.toHexString(Integer.parseInt(now.substring(10, 12)));
		if (min.length() == 1)
			min = '0' + min;
		ss = Integer.toHexString(Integer.parseInt(now.substring(12, 14)));
		if (ss.length() == 1)
			ss = '0' + ss;
		sbTime.append("F4");
		sbTime.append(year);
		sbTime.append(month);
		sbTime.append(day);
		sbTime.append(hour);
		sbTime.append(min);
		sbTime.append(ss);
		sbTime.append("00");
		now = sbTime.toString().toUpperCase();
		return now;
	}

	/**
	 * 取得String字符的前两位
	 */
	public static String getTag(String data) {
		if (!data.isEmpty()) {
			resultString = data.substring(0, 2);
		}
		return resultString;
	}

	/**
	 * 去除String字符中所有的空白符
	 */
	public static String getTrimData(String data) {
		if (!data.isEmpty()) {
			resultTrimData = data.replaceAll("\\s*", "");
		}
		return resultTrimData;
	}

	/**
	 * byte[]转变为16进制String字符, 每个字节2位, 不足补0
	 */
	public static String getStringByBytes(byte[] bytes) {
		String result = null;
		String hex = null;
		if (bytes != null && bytes.length > 0) {
			final StringBuilder stringBuilder = new StringBuilder(bytes.length);
			for (byte byteChar : bytes) {
				hex = Integer.toHexString(byteChar & 0xFF);
				if (hex.length() == 1) {
					hex = '0' + hex;
				}
				stringBuilder.append(hex.toUpperCase());
			}
			result = stringBuilder.toString();
		}
		return result;
	}

	/**
	 * 把16进制String字符转变为byte[]
	 */
	public static byte[] getBytesByString(String data) {
		byte[] bytes = null;
		if (data != null) {
			data = data.toUpperCase();
			int length = data.length() / 2;
			char[] dataChars = data.toCharArray();
			bytes = new byte[length];
			for (int i = 0; i < length; i++) {
				int pos = i * 2;
				bytes[i] = (byte) (charToByte(dataChars[pos]) << 4 | charToByte(dataChars[pos + 1]));
			}
		}
		return bytes;
	}

	/**
	 * 取得在16进制字符串中各char所代表的16进制数
	 */
	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}

	/**
	 * 方法介绍
	 */
	public static void helpInfo() {
		System.out.println("十进制转成十六进制: Integer.toHexString(int i) ");
		System.out.println("十六进制转成十进制: Integer.valueOf('FFFF',16).toString() ");
	}

	/**
	 * 根据传入的两个double类型的时间戳, 后者比较大, 算出他们之间隔了多少小时
	 */
	public static double getHours(double date1, double date2) {
		double hours = (date2 - date1) / (1000 * 60 * 60);
		hours = Double.parseDouble(df.format(hours));
		// hours = Math.round((hours * 100) / 100);
		return hours;
	}

	/**
	 * 根据传入的时间mills和距离(千米)得出速度
	 */
	public static double getSpeed(double mills, double dist) {
		double hours = 0;
		hours = (Double) (mills * 0.001 / 60 / 60);
		if (hours == 0) {
			return 0;
		}
		double speed = dist / hours;
		speed = Double.valueOf(df.format(speed));
		// hours = double.valueOf(df.format(hours));
		return speed;
	}

	/**
	 * 根据用户传入的身高与步数, 返回用户步行的里程
	 */
	public static String getDisc(String height, String stepCount) {
		String dist = "0";
		double distance_dou = Double.valueOf(stepCount)
				* Double.valueOf(height) * 0.45 * 0.01 * 0.001;
		String distance_str = df.format(distance_dou);
		return distance_str;
	}

	/**
	 * 根据传入的当前步数与目标步数，计算完成的百分比
	 */
	public static String getPercent(String currentStep, String targetStep) {
		double percent = Double.valueOf(currentStep)
				/ Double.valueOf(targetStep) * 100;
		return df.format(percent);
	}

	/**
	 * 根据传入的2个字节4位16进制字符比如FFFF, 计算返回int类型的绝对值
	 */
	public static int hexStringX2bytesToInt(String hexString) {
		return binaryString2int(hexString2binaryString(hexString));
	}

	/**
	 * 16进制转换为2进制
	 */
	public static String hexString2binaryString(String hexString) {
		if (hexString == null || hexString.length() % 2 != 0) {
			return null;
		}
		String bString = "", tmp;
		for (int i = 0; i < hexString.length(); i++) {
			tmp = "0000"
					+ Integer.toBinaryString(Integer.parseInt(
					hexString.substring(i, i + 1), 16));
			bString += tmp.substring(tmp.length() - 4);
		}
		return bString;
	}

	/**
	 * 二进制转为10进制 返回int
	 */
	public static int binaryString2int(String binarysString) {
		if (binarysString == null || binarysString.length() % 8 != 0) {
			return 0;
		}
		int result = Integer.valueOf(binarysString, 2);
		if ("1".equals(binarysString.substring(0, 1))) {
			System.out.println("这是个负数");
			char[] values = binarysString.toCharArray();
			for (int i = 0; i < values.length; i++) {
				if (values[i] == '1') {
					values[i] = '0';
				} else {
					values[i] = '1';
				}
			}
			binarysString = String.valueOf(values);
			result = Integer.valueOf(binarysString, 2) + 1;
		}

		return result;
	}

	/**
	 *
	 * 二进制转为16进制
	 */
	public static String binaryString2hexString(String bString) {
		if (bString == null || bString.equals("") || bString.length() % 8 != 0) {
			return null;
		}
		StringBuffer tmp = new StringBuffer();
		int iTmp = 0;
		for (int i = 0; i < bString.length(); i += 4) {
			iTmp = 0;
			for (int j = 0; j < 4; j++) {
				iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
			}
			tmp.append(Integer.toHexString(iTmp));
		}
		return tmp.toString();
	}

	/**
	 * 根据传入的X, Y, Z 算出 x平方 + y平方 + c 平方 的平方根值
	 */
	public static int getXYZsquareRoot(int x, int y, int z) {
		return (int) Math.sqrt(x * x + y * y + z * z);
	}

	/**
	 * 取当前的时间, 返回int型的小时, 比如 23:59:59 返回 23的int
	 */
	public static int getCurrentHour() {
		Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY);
		return hour;
	}

	/**
	 * 取得当前的分钟数
	 * @return
	 */
	public static int getCurrentMinute() {
		Calendar c = Calendar.getInstance();
		int minute = c.get(Calendar.MINUTE);
		return minute;
	}

	/**
	 * 取得当前的秒数
	 */
	public static int getCurrentSecond() {
		Calendar c = Calendar.getInstance();
		int minute = c.get(Calendar.SECOND);
		return minute;
	}

}
